<?php

namespace App\Http\Services;
/**
 * Created by PhpStorm.
 * User: thorjiang
 * Date: 2018/5/10
 * Time: 14:40
 */
use App\Models\FacilityModel;

/**
 * Class FacilityDTOBuilderService
 * @package App\Http\Services
 */
class FacilityDTOBuilderService
{
    /**
     * 格式化返回值
     * @param $userId
     * @param $tickets
     * @return mixed
     */
    public static function buildList($facilities)
    {
        $data = [];
        foreach ($facilities as $facility) {
            $ret = static::build($facility);
            $data[] = $ret;
        }

        return $data;
    }

    /**
     * @param $facility
     * @return array
     */
    public static function build($facility)
    {
        if (!empty($facility['images'])) {
            $images = json_decode($facility['images'], true);
            $host = config('app.url');
            $newArr = array_map(function ($image) use ($host) {
                return $host . '/' . $image;
            }, $images);

            $facility['images'] = $newArr;
        } else {
            $facility['images'] = [];
        }
        switch ($facility['category_id']) {
            case CATEGORY_BUSINESS:
                if (!empty($facility['brand'])) {
                    $facility['brand'] = json_decode($facility['brand'], true);
                } else {
                    $facility['brand'] = new \stdClass();
                }

                if (!empty($facility['food_brand'])) {
                    $facility['food_brand'] = json_decode($facility['food_brand'], true);
                } else {
                    $facility['food_brand'] = new \stdClass();
                }
                break;
            case CATEGORY_HOTEL:
                if (!empty($facility['price'])) {
                    $facility['price'] = json_decode($facility['price'], true);
                } else {
                    $facility['price'] = new \stdClass();
                }
                break;
            case CATEGORY_SHIP:
                if (!empty($facility['price'])) {
                    $facility['price'] = json_decode($facility['price'], true);
                } else {
                    $facility['price'] = new \stdClass();
                }
                break;
            default:
                return $facility;
        }

        return $facility;
    }
}
